package com.soyorin.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@SpringBootApplication
@ComponentScan({"com.soyorin.order.config",
        "com.soyorin.order.controller",
        "com.soyorin.order.service"})
@MapperScan("com.soyorin.order.mapper")
public class SoyoOrderApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context =
                SpringApplication.run(SoyoOrderApplication.class, args);
        Environment env = context.getEnvironment();
        String dbUrl = env.getProperty("spring.datasource.url");
        String dbUser = env.getProperty("spring.datasource.username");
        String dbPass = env.getProperty("spring.datasource.password");
        System.out.printf("数据库连接信息[url:%s 用户名:%s 密码:%s]%n",dbUrl, dbUser, dbPass);
        try (Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPass)) {
            System.out.println("MySQL数据库连接成功");
        } catch (SQLException e) {
            System.err.println("MySQL数据库连接失败! 错误信息:");
            e.printStackTrace();
            System.exit(1);
        }
        String port = env.getProperty("server.port");
        System.out.println("Soyo-Order订单服务启动成功!访问端口:"+port+"以调用服务");
    }

}
